package com.soft.idc.module.flow;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.jetty.util.log.Log;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.CaptchaRender;
import com.soft.idc.common.control.AuthInterceptor;
import com.soft.idc.common.model.IdcOrg;
import com.soft.idc.common.model.IdcRole;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.util.JQList;
import com.soft.idc.common.util.JQPage;

public class FlowController extends Controller {

	public void index() {
		IdcOrg org = (IdcOrg) this.getSessionAttr("SYS_USER_ORG");

		this.renderJsp("/3gflow/main.jsp");
	}

	public void loadFlow() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");

		IdcOrg org = this.getSessionAttr("SYS_USER_ORG");
		String iccid = this.getPara("iccid");
		String planeCode = this.getPara("planeCode");
		
		String sqlQuery = "select d.device_id,d.code,(select code from idc_airplane a where a.plane_id = d.plane_id) as plane_code ,"
				+ "(select sum(r.value) from idc_kpi_record r where r.obj_id = d.device_id and r.kpi_id = 100160 and r.obj_type = 0 and r.create_time >= date_format(now(),'%Y-%m')) as value,"
				+ " (select count(1) from idc_warn_record wr where wr.obj_id = d.device_id and wr.obj_type = 0 and wr.is_clear = 0 ) as warncount ";
		String sqlFrom = " from idc_device d "
				+ "where  d.type_id = 110  and d.org_id = ? ";

		List<Object> list = new ArrayList<Object>();
		list.add(org.getOrgId());
		if (iccid != null && !"".equals(iccid)) {
			sqlFrom += " and d.code = ?";
			list.add(iccid);
		}
		if (planeCode != null && !"".equals(planeCode)) {
			sqlFrom += " and d.plane_id = (select plane_id from idc_airplane where code = ? limit 1)";
			list.add(planeCode);
		}

		int pageNum = start / length + 1;
		Page<Record> page = Db.paginate(pageNum, length, sqlQuery, sqlFrom , (Object[]) list.toArray());
		JQPage jPage = new JQPage(page);

		this.renderJson(jPage);
	}

	public void loadDayFlow() {
		String deviceId = this.getPara("deviceId");
		String createTime = this.getPara("createTime");
		String sql = "select create_time,value from idc_kpi_record i"
				+ " where obj_id = ? and kpi_id = 100160 and create_time >= str_to_date(?,'%Y-%m-%d %H:%i:%s') order by create_time asc";
		List<Record> list = Db.find(sql, deviceId, createTime);
		this.renderJson(list);
	}

	public void loadMonthFlow() {
		String deviceId = this.getPara("deviceId");
		String createTime = this.getPara("createTime");
		String sql = "select device_id,date_format(month,'%Y-%m-01 00:00:00') as p_month,max(value) as p_value "
				+ "from idc_cardflow_month where device_id=? and create_time >= str_to_date(?,'%Y-01-01 00:00:00') group by device_id,p_month;";
		List<Record> list = Db.find(sql, deviceId, createTime);
		this.renderJson(list);
	}

}
